package com.etung.iot.demo.runner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etung.iot.demo.alarm.AlarmCacheUtil;
import com.etung.iot.demo.dao.AlarmMapper;
import com.etung.iot.demo.dao.AlarmWarningMapper;
import com.etung.iot.demo.dao.BoxMapper;
import com.etung.iot.demo.handler.UserCacheUtil;
import com.etung.iot.demo.modles.AlarmEntity;
import com.etung.iot.demo.modles.AlarmInfo;
import com.etung.iot.demo.modles.AlarmWarningEntity;
import com.etung.iot.demo.modles.BoxEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

@Component
@Slf4j
public class AlarmRunner implements Runnable {

    public static LinkedBlockingQueue queue = new LinkedBlockingQueue<AlarmInfo>();
    @Autowired
    private AlarmMapper alarmMapper;
    @Autowired
    private BoxMapper boxMapper;
    @Autowired
    private AlarmWarningMapper alarmWarningMapper;
    @Autowired
    private AlarmCacheUtil alarmCacheUtil;
    @Autowired
    private UserCacheUtil userCacheUtil;

    @Override
    public void run() {
        log.info("AlarmRunner-------Thread:" + Thread.currentThread().getName());
        while (true) {
            AlarmInfo alarmInfo = null;
            try {
                alarmInfo = (AlarmInfo) queue.take();
                AlarmEntity alarmEntity = alarmCacheUtil.getAlarmEntity(alarmInfo.getAlarmId(),alarmInfo.getImei());
                //获取box信息
                BoxEntity boxEntity = alarmCacheUtil.getBoxEntity(alarmInfo.getImei());
                if (alarmEntity != null && boxEntity != null){
                    if(alarmInfo.getValue().equals("0")){//解除报警
                        List<AlarmWarningEntity> alarmWarningEntities = alarmWarningMapper.selectList(new QueryWrapper<AlarmWarningEntity>()
                                .eq("alarm_id", alarmInfo.getAlarmId())
                                .eq("box_id", boxEntity.getId())
                                .eq("ack_flag", 0)
                        );
                        if (alarmWarningEntities != null && alarmWarningEntities.size() >0){
                            alarmWarningEntities.forEach((item)->{
                                item.setAckFlag(1);
                            });
                        }
                    }else{//新增报警
                        AlarmWarningEntity alarmWarningEntity = new AlarmWarningEntity();
                        alarmWarningEntity.setUserId(userCacheUtil.getUid(boxEntity.getImei()));
                        alarmWarningEntity.setProjectId(boxEntity.getProjectId());
                        alarmWarningEntity.setBoxId(boxEntity.getId());
                        alarmWarningEntity.setAlarmId(alarmInfo.getAlarmId());
                        alarmWarningEntity.setSerious(alarmEntity.getType());
                        alarmWarningEntity.setTime(alarmInfo.getTime());
                        alarmWarningEntity.setAckFlag(0);
                        alarmWarningMapper.insert(alarmWarningEntity);
                        System.out.println(alarmEntity.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
